package com.market.web.controller;

import java.util.List;
import java.util.Random;

import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.market.domain.Picture;
import com.market.manager.PictureManager;
import com.market.web.model.MarketList;
import com.market.web.model.PictureModel;

@Controller("pictureController")
@Scope("session")
public class PictureController extends
		GenericController<PictureManager, PictureModel, Picture> {

	@Override
	@Autowired
	protected void setModel(PictureModel model) {
		this.model = model;
	}

	@Override
	@Autowired
	protected void setManager(PictureManager manager) {
		this.manager = manager;
	}
	
	
	
	public String getRandomMusic(){
		Random r  = new Random();
		return "../resources/music/music" + r.nextInt(5) + ".mp3";
	}

	public void loadHomePagePictures() {
		if (this.model.getHomePagePictures() == null) {
			this.model.setHomePagePictures(this.manager
					.fetchAllHomePagePictures());
		}
	}

	public void load2010Picture() {
		List<Picture> pictures= this.manager.fetchPictureByYear("2010");
		this.model.setList(pictures);
	}

	public void load2011Picture() {
		List<Picture> pictures= this.manager.fetchPictureByYear("2011");
		this.model.setList(pictures);
	}

	public void load2012Picture() {
		List<Picture> pictures= this.manager.fetchPictureByYear("2012");
		this.model.setList(pictures);
	}
	
	public void load2013Picture() {
		List<Picture> pictures= this.manager.fetchPictureByYear("2013");
		this.model.setList(pictures);
	}
	
	
	public String loadGalleryPictures(){
		List<Picture> pictures= this.manager.fetchPictureByYear(this.model.getGalleryYear());
		this.model.setList(pictures);
		return "LOAD GALLERY PICTURES SUCCEED";
		
	}

	@Override
	protected void loadModel() {
		List<Picture> all = manager.fetchAllNotRecycled();
		if (all.size() > 0) {
			model.setList(all);
			this.refreshCurrent();
		}
		this.model.setIsInitialized(true);
	}

	@Override
	public void deleteCurrent() {
		super.deleteCurrent();
		// this.deleteFile();
	}

	/**
	 * When user choose another file for upload, set that file name to Picture's
	 * fileName property
	 */
	public void currentUploadedFileChange() {
		Picture pic = this.model.getCurrent();
		if (pic.getUploadedFile() != null) {
			pic.setFileName(pic.getUploadedFile().getFileName());
		}
	}

}
